/**
 * Created by lintao_alex on 2017/9/19.
 */

namespace game{
    import IClear = mengdao.IClear;
    import TimeGetter = mengdao.TimeGetter;

    export class NetCallNode implements IClear{
        private static _CREATE_CNT:number=0;

        readonly id:number;
        public command:string;
        private _request:any;
        private _callObj:any;
        private _responseCall:Function;
        private _errorCall:Function;
        private _sended:boolean;

        public constructor(){
            this.id = NetCallNode._CREATE_CNT++;
        }

        public get sended(){
            return this._sended;
        }

        public set sended(v){
            this._sended = v;
        }

        public reset(request:any, callObj?:any, responseCall?:Function, errorCall?:Function){
            this._request = request;
            this._callObj = callObj;
            this._responseCall = responseCall;
            this._errorCall = errorCall;
            this._sended = false;
        }

        public invokeResponse(response:any){
            if(response["serverTime"] && response["serverTime"]>0)
                TimeGetter.serverTime = response["serverTime"];
            if(this._responseCall){
                this._responseCall.call(this._callObj, response, this.request);
            }
        }

        public errorResponse(err:any){
            //提示粮仓中还有存粮
            if(this._errorCall){
                this._errorCall.call(this._callObj, err);
            }
        }

        get request():any{
            return this._request;
        }

        clear(){
            this.command = null;
            this._request = null;
            this._callObj = null;
            this._responseCall = null;
            this._errorCall = null;
        }
    }
}